package com.smart.upms.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.base.BaseController;
import com.smart.core.starter.model.R;
import com.smart.upms.sys.param.SysDeptParam;
import com.smart.upms.sys.param.SysDeptQueryParam;
import com.smart.upms.sys.result.DeptTreeResult;
import com.smart.upms.sys.result.SysDeptResult;
import com.smart.upms.sys.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Api(description = "部门管理接口")
@RestController
@RequestMapping("/sys/sys-dept")
@AllArgsConstructor
public class SysDeptController extends BaseController {
    private final ISysDeptService sysDeptService;


    /**
     * 添加部门
     *
     * @param param 部门参数
     * @return success/false
     */
    @ApiOperation(value = "添加部门")
    @PostMapping
    public R<Boolean> add(@Validated @RequestBody SysDeptParam param) {
        return R.success(sysDeptService.save(param));
    }


    /**
     * 修改部门
     *
     * @param param 部门信息
     * @return success/false
     */
    @ApiOperation(value = "修改部门")
    @PutMapping
    public R<Boolean> update(@Validated @RequestBody SysDeptParam param) {
        return R.success(sysDeptService.update(param));
    }

    /**
     * 删除部门
     *
     * @param id ID
     * @return success/false
     */
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable @Min(1L) Long id) {
        return R.success(sysDeptService.delete(id));
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public R<SysDeptResult> get(@PathVariable @Min(1L) Long id) {
        SysDeptResult sysDeptResult = sysDeptService.get(id);
        return R.success(sysDeptResult);
    }

    /**
     * 返回部门树
     *
     * @return 树形菜单
     */
    @ApiOperation(value = "获取部门树")
    @GetMapping(value = "/tree")
    public R<List<DeptTreeResult>> getTree() {
        return R.success(sysDeptService.selectListTree());
    }


    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<Page<SysDeptResult>> page(SysDeptQueryParam param) {
        Page<SysDeptResult> page = getPage(param);
        return R.success(sysDeptService.listPage(page, param));
    }

}
